package communication.common.business.util;

import communication.common.business.base.MsgData;
import communication.common.business.base.WorkModeEnum;
import communication.common.business.heartbeat.HeartBeat;
import communication.common.business.heartbeat.HeartBeatAck;
import communication.common.business.hole.P2pHoleRequest;
import communication.common.business.user.request.LoginRequest;
import communication.common.business.user.request.RegistryRequest;
import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * @author zxl
 */
@Slf4j
public class MsgDataUtil {


    /**
     *   发送注册信息
     * @return
     */
    public static MsgData createRegistryMsgData(){
        RegistryRequest request= RegistryRequest
                .builder()
                .accountName("zxl")
                .gender(0)
                .passWord("12345")
                .phone("1111")
                .build();

        return MsgData
                .builder()
                .module("user")
                .op("registry")
                .type(0)
                .opParam(new Object[]{request})
                .build();
    }

    public static MsgData createRegistryMsgData2(){
        RegistryRequest request= RegistryRequest
                .builder()
                .accountName("zxl2")
                .gender(0)
                .passWord("12345")
                .phone("2222")
                .build();

        return MsgData
                .builder()
                .module("user")
                .op("registry")
                .type(0)
                .opParam(new Object[]{request})
                .build();
    }

    /**
     *   发送登录信息
     * @return
     */
    public static MsgData createLoginMsgData(){
        LoginRequest request=LoginRequest.builder()
                .accountName("zxl")
                .passWord("12345")
                .build();

        return MsgData
                .builder()
                .module("user")
                .op("loginByAccountName")
                .type(0)
                .opParam(new Object[]{request})
                .build();
    }


    /**
     *   发送登录信息
     * @return
     */
    public  static MsgData createLoginMsgData2(){
        LoginRequest request=LoginRequest.builder()
                .accountName("zxl2")
                .passWord("12345")
                .build();

        return MsgData
                .builder()
                .module("user")
                .op("loginByAccountName")
                .type(0)
                .opParam(new Object[]{request})
                .build();
    }

    /**
     *
     * @return
     */
    public static MsgData createTransferHoleRequest(String host,int port){
        P2pHoleRequest request=P2pHoleRequest.builder()
                .targetUserId(1L)
                .sourceUserId(2L)
                .type(0)
                .receiveDevice(host+":"+port)
                .build();

        return MsgData
                .builder()
                .module("net")
                .op("hole")
                .type(0)
                .opParam(new Object[]{request})
                .build();
    }

    public static MsgData createHoleRequest(P2pHoleRequest request){
        return MsgData
                .builder()
                .module("net")
                .op("hole")
                .type(request.getType())
                .opParam(new Object[]{request})
                .build();
    }


    public static MsgData createChatMsgData(String msg) {
        return MsgData
                .builder()
                .module("chat")
                .op("msg")
                .type(0)
                .opParam(new Object[]{msg})
                .build();

    }


    public static MsgData createHeartBeatAck(HeartBeatAck ack){
        return MsgData.builder()
                .module("net")
                .op("heartBeatAck")
                .opParam(new Object[]{ack})
                .response(null)
                .build();
    }


    public static MsgData createHeartBeat(HeartBeat heartBeat){
        return MsgData.builder()
                    .op("heartBeat")
                    .module("net")
                    .opParam(new Object[]{heartBeat})
                    .type(0)
                    .response(null)
                    .build();
    }


    public static MsgData createHoleReq(Integer weight){
        return MsgData.builder()
                .op("test-hole")
                .module("net")
                .opParam(new Object[]{weight})
                .type(0)
                .response(null)
                .build();
    }

    public static MsgData createHoleResp(WorkModeEnum workModeEnum){
        return MsgData.builder()
                .op("test-hole")
                .module("net")
                .opParam(null)
                .type(1)
                .response(workModeEnum)
                .build();
    }


    public static byte[] obj2ByteArray(Object obj) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream sOut = new ObjectOutputStream(out);
        sOut.writeObject(obj);
        sOut.flush();
        return out.toByteArray();
    }

    public static MsgData byteArray2obj(byte[] bytes) throws IOException, ClassNotFoundException {
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        ObjectInputStream sIn = new ObjectInputStream(in);
        return (MsgData)sIn.readObject();
    }

}
